# -*- perl -*-

use Test::More tests => 2;
use HTML::FormFu;
use HTML::FormFu::DBIC ':all';
use lib 't/lib';
use DBSchema;
use YAML::Syck qw( Load );

my $dsn    = 'dbi:SQLite:dbname=t/var/dvdzbr.db';
my $schema = DBSchema->connect( $dsn, '', '', {} );
$schema->deploy({ add_drop_table => 1, });
$schema->populate('User', [
    [ qw/id username name password / ],
    [ 1, 'jgda', 'Jonas Alves', ''],
    [ 2, 'isa' , 'Isa', '', ],
    [ 3, 'zby' , 'Zbyszek Lukasiak', ''],
    ]
);
$schema->populate('Tag', [
    [ qw/id name file / ],
    [ 1, 'comedy', '' ],
    [ 2, 'dramat', '' ],
    [ 3, 'australian', '' ],
    ]
);
$schema->populate('Dvd', [
    [ qw/id name imdb_id owner current_borrower creation_date alter_date / ],
    [ 1, 'Picnick under the Hanging Rock', 123, 1, 1, '', '' ],
    [ 2, 'The Deerhunter', 1234, 1, 1, '', '' ],
    [ 3, 'Rejs', 1235, 1, 1, '', '' ],
    [ 4, 'Seksmisja', 1236, 1, 1, '', '' ],
    ]
); 
$schema->populate( 'Dvdtag', [
    [ qw/ dvd tag / ],
    [ 1, 2 ],
    [ 1, 3 ],
    [ 3, 1 ],
    [ 4, 1 ],
    ]
);

my $form = HTML::FormFu->new( { stash => { schema => $schema } } );
$form->populate( Load( do { local $/; <DATA> } ) );
my $dvd = $schema->resultset( 'Dvd' )->find( 1 );
fill_formfu_values( $dvd, $form );
my $user_field = $form->get_element( { name => 'owner' } );
is ( $user_field->default, 1, 'User loaded' );
my $tags_field = $form->get_element( { name => 'tags' } );
is_deeply ( $tags_field->default, [ '2', '3' ], 'Tags loaded' );

__DATA__

---
elements:
   - type: Select::DBIC
     name: owner
     dbic_args: 
       resultset_name: User
       search_attr: 
         order_by: 
           - id
   - type: Select::DBIC
     name: tags
     dbic_args: 
       resultset_name: Tag
       presentation: name
       search_attr: 
         order_by: 
           - id

